//--------------------------------------------------------------------------------
// This file is part of the Windows Workflow Foundation Sample Code
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// This source code is intended only as a supplement to Microsoft
// Development Tools and/or on-line documentation.  See these other
// materials for detailed information regarding Microsoft code samples.
// 
// THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//--------------------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.Globalization;
using System.Collections;
using System.Diagnostics;

namespace Microsoft.Samples.Workflow.UI.Design
{
    class ReadonlyPropertyDescriptor : PropertyDescriptor
    {
        private PropertyDescriptor realPropertyDescriptor;

        internal ReadonlyPropertyDescriptor(PropertyDescriptor descriptor)
            : base(descriptor, null)
        {
            this.realPropertyDescriptor = descriptor;
        }

        public override string Category
        {
            get
            {
                return this.realPropertyDescriptor.Category;
            }
        }
        public override AttributeCollection Attributes
        {
            get
            {
                ArrayList collection = new ArrayList();
                foreach (Attribute attribute in this.realPropertyDescriptor.Attributes)
                {
                    //should not have any editor attribute and only one readonly attribute
                    if (!(attribute is EditorAttribute || attribute is ReadOnlyAttribute))
                    {
                        collection.Add(attribute);
                    }
                }
                collection.Add(new ReadOnlyAttribute(true));
                AttributeCollection newCollection = new AttributeCollection((Attribute[])collection.ToArray(typeof(Attribute)));
                return newCollection;
            }
        }
        public override TypeConverter Converter
        {
            get
            {
                return this.realPropertyDescriptor.Converter;
            }
        }
        public override string Description
        {
            get
            {
                return this.realPropertyDescriptor.Description;
            }
        }
        public override Type ComponentType
        {
            get
            {
                return this.realPropertyDescriptor.ComponentType;
            }
        }
        public override Type PropertyType
        {
            get
            {
                return this.realPropertyDescriptor.PropertyType;
            }
        }
        public override bool IsReadOnly
        {
            get
            {
                return true;
            }
        }
        public override void ResetValue(object component)
        {
            this.realPropertyDescriptor.ResetValue(component);
        }
        public override bool CanResetValue(object component)
        {
            return false;
        }
        public override bool ShouldSerializeValue(object component)
        {
            return this.realPropertyDescriptor.ShouldSerializeValue(component);
        }
        public override object GetValue(object component)
        {
            return this.realPropertyDescriptor.GetValue(component);
        }
        public override void SetValue(object component, object value)
        {
            //This is readonly property descriptor
            Debug.Assert(false, "SetValue should not be called on readonly property!");
        }
    }
}
